﻿using UnityEngine;
using System.Collections;


    [ExecuteInEditMode]

    public delegate void OnMoveStart();
    public delegate void OnMoveEnd();
    public delegate void OnMove(Vector2 move);

public class TouchKit : MonoBehaviour
{
    public Vector3 touchPos;//按住的位置
    public Vector3 currentPos;//当前的位置
    public int height;
    public int width;

    public bool touchScreen;

    public Vector3 changeVec;
    public int innerChangeDis = 50;

    public GameObject content;
    public GameObject centerIcon;


    public static OnMoveStart On_MoveStart;
    public static OnMoveEnd On_MoveEnd;
    public static OnMove On_Move;


    private static TouchKit inst;
    public static TouchKit Instance
    {
        get
        {
            return inst;
        }
    }

    void Awake()
    {
        inst = this;
    }


    void LateUpdate()
    {
        if(OverThisFrame())
        {
            return;
        }

        TouchScreen();
        CalcDirVec();
        
    }
    

    bool OverThisFrame()
    {
        content.SetActive(false);

     

        if (Input.GetMouseButtonDown(0)) //点击到人生身上屏蔽
        {
            Ray ray = CameraTool.Instance.sceneCamera.ScreenPointToRay(Input.mousePosition);

            RaycastHit hit;

            Transform tr = null;

            if (Physics.Raycast(ray, out hit))
            {
                tr = hit.transform;
            }

            if (tr != null && !UICamera.Raycast(Input.mousePosition))
            {
                var charactor = tr.GetComponent<Room.RoomCharactor>();

                if (charactor != null)
                {

                    var pos = UICamera.mainCamera.ScreenToWorldPoint(Input.mousePosition);

                    Tool.ScreenEffectTool.Instance.OnClick0(pos);
                    return true;
                }
            }
        }

        return false;
    }

    void TouchScreen()
    {

        if (Input.GetKey(KeyCode.Mouse0))
        {
            if (!touchScreen)
            {
                if(CastOverUICamera())
                {
                    return;
                }

                touchPos = Input.mousePosition;
                transform.position = UICamera.mainCamera.ScreenToWorldPoint(touchPos);
            }
            touchScreen = true;
            
        }
        else
        {
            touchScreen = false;
            transform.position = Vector3.zero;
        }

        if (touchScreen)
        {
            currentPos = Input.mousePosition;
            if (On_MoveStart != null)
            {
                On_MoveStart();
                
            }
        }
        else
        {
            if (On_MoveEnd != null)
            {
                On_MoveEnd();
            }
            centerIcon.transform.localPosition = Vector3.zero;
            currentPos = touchPos = Vector3.zero;
            
        }

        
    }

    /// <summary>
    /// 计算方向向量
    /// </summary>
    void CalcDirVec()
    {
        if (touchScreen)
        {
            changeVec = currentPos - touchPos;

            var inChange = changeVec;

            if (inChange.magnitude > innerChangeDis)
            {
                inChange = inChange.normalized * innerChangeDis;
            }

            centerIcon.transform.localPosition = inChange;

            if (On_Move != null && !(changeVec.x == 0f && changeVec.y == 0f))
            {
                content.SetActive(true);
                On_Move(new Vector2(changeVec.x, changeVec.y));
            }
            else
            {
                changeVec = Vector3.zero;
            }
        }
        else
        {
            changeVec = Vector3.zero;
        }
    }

    bool CastOverUICamera()
    {

        //Ray ray = UICamera.mainCamera.ScreenPointToRay(Input.mousePosition);

        if (UICamera.Raycast(Input.mousePosition))
        {
            return true;
        }
        return false;

    }

    void OnEnable()
    {

    }

    void OnDisable()
    {
        if (touchScreen)
        {
            touchScreen = false;
            if (On_MoveEnd != null)
            {
                On_MoveEnd();
            }
            changeVec = Vector3.zero;
            touchPos = Vector3.zero;
            currentPos = Vector3.zero;
        }
    }

    #region 工具
    static public void SetEnabled(bool enable)
    {
        inst.gameObject.SetActive(enable);
    }

    static public bool GetEnabled()
    {
        return inst.gameObject.activeSelf;
    }
    #endregion
}

